package me.keiwu.user.dao;

import me.keiwu.user.domain.Notice;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

/**
 * Created by kei on 02/02/2018.
 */
public interface NoticeDao extends JpaRepository<Notice, Long>, JpaSpecificationExecutor<Notice> {


    @Query(value = "select count(*) from notice where to_user=?1 and has_read=0", nativeQuery = true)
    int countNotRead(Long userId);


    @Query(value = "select count(*) from notice where to_user=?2 and notice_type=?1 and has_read=0", nativeQuery = true)
    int countNotReadByNoticeType(Integer noticeType, Long userId);


    @Query(value = "select * from notice where notice_type=?1 and from_user=?2 and to_user=?3 limit 1", nativeQuery = true)
    Notice findForReNotice(Integer noticeType, Long fromUser, Long toUser);


    @Query(value = "select * from notice where notice_type=?1 and unique_key=?2 and from_user=?3 and to_user=?4 limit 1", nativeQuery = true)
    Notice findUniqueNotice(Integer noticeType, String uniqueKey, Long fromUser, Long toUser);

}
